package com.holly.unit.deform.modular.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.holly.unit.auth.api.context.LoginContext;
import com.holly.unit.core.pojo.response.ErrorResponseData;
import com.holly.unit.core.pojo.response.ResponseData;
import com.holly.unit.core.pojo.response.SuccessResponseData;
import com.holly.unit.db.api.pojo.page.PageResult;
import com.holly.unit.deform.api.DeformDataApi;
import com.holly.unit.deform.api.pojo.online.pojo.DesignFormDataPojo;
import com.holly.unit.deform.api.pojo.rsp.DeformResult;
import com.holly.unit.deform.entity.DesignFormData;
import com.holly.unit.deform.modular.pojo.DesignFormDataDelRequest;
import com.holly.unit.deform.modular.pojo.DesignFormDataQueryRequest;
import com.holly.unit.deform.modular.pojo.DesignFormDataRequest;
import com.holly.unit.scanner.api.annotation.ApiResource;
import com.holly.unit.scanner.api.annotation.GetResource;
import com.holly.unit.scanner.api.annotation.PostResource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.HtmlUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;

/**
 * 类描述: 表单设计数据 控制器
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/4/11 9:44
 */
@RestController
@ApiResource(name = "表单设计数据")
@Api(tags = "表单设计数据")
public class DeformDataController {

    @Resource
    private DeformDataApi<DesignFormData> api;

    /**
     * 分页列表查询（主表）
     *
     * @param request
     * @param req
     * @return
     */
    @ApiOperation(value = "分页列表查询" ,notes = "分页列表查询")
    @GetResource(name = "分页列表查询" , path = "/designFormData/list")
    public ResponseData queryPageList(DesignFormDataRequest request,
                                      HttpServletRequest req) throws UnsupportedEncodingException {
        String username = LoginContext.me().getLoginUser().getAccount();
        DesignFormData designFormData = new DesignFormData();
        BeanUtil.copyProperties(request,designFormData);
        if(request!=null &&  StrUtil.isNotEmpty(request.getSuperQuery())){
            request.setSuperQuery(HtmlUtils.htmlUnescape(request.getSuperQuery()));
        }
        DeformResult<PageResult<DesignFormData>>  res = api.queryPage(designFormData,request.getDesformCode(), request.getPageNo(), request.getPageSize(), req.getParameterMap(), request.getSuperQuery(), username);
        PageResult<DesignFormData> pageResult = res.getResult();
        return new SuccessResponseData(pageResult);
    }

    /**
     * 新增
     *
     * @param designFormData
     * @return
     */
    @ApiOperation(value = "新增" ,notes = "新增")
    @PostResource(name = "新增" , path = "/designFormData/add")
    public ResponseData add(@RequestBody DesignFormData designFormData) {
        if(designFormData!=null && StrUtil.isNotEmpty(designFormData.getDesformDataJson())){
            designFormData.setDesformDataJson(HtmlUtils.htmlUnescape(designFormData.getDesformDataJson()));
        }
        DeformResult deformResult = api.addOne(designFormData);
        if (deformResult.isSuccess()) {
            return new SuccessResponseData(deformResult.getResult());
        } else {
            return new ErrorResponseData(deformResult.getCode().toString(),deformResult.getMessage(),deformResult.getResult());
        }
    }

    /**
     * 编辑
     *
     * @param designFormData
     * @return
     */
    @ApiOperation(value = "编辑" ,notes = "编辑")
    @PostResource(name = "编辑" , path = "/designFormData/edit")
    public ResponseData edit(@RequestBody DesignFormData designFormData) {
        if(designFormData!=null && StrUtil.isNotEmpty(designFormData.getDesformDataJson())){
            designFormData.setDesformDataJson(HtmlUtils.htmlUnescape(designFormData.getDesformDataJson()));
        }
        DeformResult deformResult = api.editOne(designFormData);
        if (deformResult.isSuccess()) {
            return new SuccessResponseData(deformResult.getResult());
        } else {
            return new ErrorResponseData(deformResult.getCode().toString(),deformResult.getMessage(),deformResult.getResult());
        }
    }

    /**
     * 通过id删除
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "通过id删除" ,notes = "通过id删除")
    @PostResource(name = "通过id删除" , path = "/designFormData/delete")
    public ResponseData delete(@RequestBody @Validated(DesignFormDataDelRequest.delete.class) DesignFormDataDelRequest request) {
        DeformResult deformResult = api.deleteOne(request.getId(), LoginContext.me().getToken());
        if (deformResult.isSuccess()) {
            return new SuccessResponseData(deformResult.getResult());
        } else {
            return new ErrorResponseData(deformResult.getCode().toString(),deformResult.getMessage(),deformResult.getResult());
        }
    }


    /**
     * 批量删除
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "批量删除" ,notes = "批量删除")
    @PostResource(name = "批量删除" , path = "/designFormData/deleteBatch")
    public ResponseData deleteBatch(@RequestBody @Validated(DesignFormDataDelRequest.batchDelete.class) DesignFormDataDelRequest request) {
        String ids = request.getIds();
        this.api.deleteBatchMain(Arrays.asList(ids.split(",")), LoginContext.me().getToken());
        return new SuccessResponseData();
    }

    /**
     * 通过id查询
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "通过id查询" ,notes = "通过id查询")
    @GetResource(name = "通过id查询" , path = "/designFormData/queryById")
    public ResponseData queryById(@Validated(DesignFormDataQueryRequest.queryById.class) DesignFormDataQueryRequest request) {
        DesignFormData designForm = api.getById(request.getId());
        return new SuccessResponseData(designForm);
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @ApiOperation(value = "通过id查询" ,notes = "通过id查询")
    @GetResource(name = "通过id查询" , path = "/designFormData/queryDesignFormDataByMainId")
    public ResponseData queryDesignFormDataListByMainId(@RequestParam(name = "id", required = true) String id) {
        List<DesignFormData> designFormDataList = api.selectByMainId(id);
        return new SuccessResponseData(designFormDataList);
    }




    /**
     * 新增表单数据
     * */
    @ApiOperation(value = "新增表单数据" ,notes = "新增表单数据")
    @PostResource(name = "新增表单数据" , path = "/designFormData/addFormData")
    public ResponseData addFormData(@RequestBody @Validated(DesignFormDataPojo.addFormData.class)  DesignFormDataPojo designFormData) {
        DeformResult deformResult = api.addFormData(designFormData);
        if (deformResult.isSuccess()) {
            return new SuccessResponseData(deformResult.getResult());
        } else {
            return new ErrorResponseData(deformResult.getCode().toString(),deformResult.getMessage(),deformResult.getResult());
        }
    }

    /**
     * 查询表单数据
     * */
    @ApiOperation(value = "查询列表" ,notes = "查询列表")
    @GetResource(name = "查询列表" , path = "/designFormData/queryFormDataList")
    public ResponseData queryFormDataList(@Validated(DesignFormDataPojo.queryList.class)  DesignFormDataPojo designFormData) {
       return new SuccessResponseData(api.queryFormDataList(designFormData));
    }

    /**
     * 删除表单数据
     * */
    @ApiOperation(value = "通过id删除" ,notes = "通过id删除")
    @PostResource(name = "通过id删除" , path = "/designFormData/delFormData")
    public ResponseData delFormData(@RequestBody @Validated(DesignFormDataPojo.delete.class)  DesignFormDataPojo designFormData) {
        DeformResult deformResult = api.delFormData(designFormData);
        if (deformResult.isSuccess()) {
            return new SuccessResponseData(deformResult.getResult());
        } else {
            return new ErrorResponseData(deformResult.getCode().toString(),deformResult.getMessage(),deformResult.getResult());
        }
    }

    /**
     * 修改表单数据
     * */
    @ApiOperation(value = "通过Id修改表单数据" ,notes = "通过Id修改表单数据")
    @PostResource(name = "通过Id修改表单数据" , path = "/designFormData/editFormData")
    public ResponseData editFormData(@RequestBody @Validated(DesignFormDataPojo.editFormData.class)  DesignFormDataPojo designFormData) {
        DeformResult deformResult = api.editFormData(designFormData);
        if (deformResult.isSuccess()) {
            return new SuccessResponseData(deformResult.getResult());
        } else {
            return new ErrorResponseData(deformResult.getCode().toString(),deformResult.getMessage(),deformResult.getResult());
        }
    }

    /**
     * 批量删除
     * @param request
     * @return
     */
    @ApiOperation(value = "批量删除" ,notes = "批量删除")
    @PostResource(name = "批量删除" , path = "/designFormData/deleteBatchForm")
    public ResponseData deleteBatchForm(@RequestBody @Validated(DesignFormDataPojo.batchDelete.class) DesignFormDataPojo request) {
        String ids = request.getIds();
        this.api.deleteBatchForm(Arrays.asList(ids.split(",")),request.getTableName());
        return new SuccessResponseData();
    }
}
